package cn.edu.seu.herald.informationplatform.dao.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.mysql.jdbc.Statement;

import cn.edu.seu.herald.informationplatform.bean.league.ActivityClass;

public class ActivityClassDao extends DataAccessObject {
	private static ActivityClassDao daoInstance=null;	
	//Constructor of LeagueDao
	protected ActivityClassDao(){	
	}
	//Get LeagueDao instance
	public static ActivityClassDao getDao(){
		if(daoInstance==null){
			daoInstance=new ActivityClassDao();
		}
		return daoInstance;
	}
	
	//add
	public int addActivityClass(ActivityClass newActivityClass)throws SQLException{
		Connection con=getConnectionLeague();
		try {
			PreparedStatement ps=con.prepareStatement("insert into lg_activity_class(class_name) values(?)",Statement.RETURN_GENERATED_KEYS);
			ps.setString(1,newActivityClass.getName());
			ps.executeUpdate();
			ResultSet rsResultSet=ps.getGeneratedKeys();
			int id=-1;
			while (rsResultSet.next()){
				id=rsResultSet.getInt(1);
			}
			closeRs(rsResultSet);
			return id;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		finally{
			closeCon(con);
			
		}
	}
	//delete
	public void deleteActivityClass(int id) throws SQLException{
		Connection con=getConnectionLeague();
		try {
			PreparedStatement ps=con.prepareStatement("delete from lg_activity_class where id=?");
			ps.setInt(1,id);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		finally{
			closeCon(con);
		}
	}
	//get ActivityClassById
	public ActivityClass getActivityClassById(int id) throws SQLException{
		Connection con=getConnectionLeague();
		try {
			ActivityClass activityClass=new ActivityClass();
			PreparedStatement ps=con.prepareStatement("select * from lg_activity_class where id=?");
			ps.setInt(1,id);
			ResultSet rs=ps.executeQuery();
			while(rs.next()){
				activityClass.setName(rs.getString(2));
			}
			closeRs(rs);
			return activityClass;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		finally{
			closeCon(con);
			
		}
	}
	//get map
	public Map<Integer,ActivityClass> getActivityClassMap()throws SQLException{
		Connection con=getConnectionLeague();
		try {
			PreparedStatement ps=con.prepareStatement("select id,class_name from lg_activity_class");
			ResultSet rsResultSet=ps.executeQuery();
			int id=0;
			String nameString=null;
			HashMap<Integer,ActivityClass> classMap=new HashMap<Integer,ActivityClass>();
			while (rsResultSet.next()) {
				id=rsResultSet.getInt(1);
				nameString=rsResultSet.getString(2);
				ActivityClass activityClass=new ActivityClass();
				activityClass.setName(nameString);
				classMap.put(Integer.valueOf(id),activityClass);
			}
			return classMap;
		}catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		finally{
			closeCon(con);
		}
	}
	
}
